package com.hx.jvm.proxy.assit;

import com.hx.jvm.proxy.jdk.Hello;
import com.hx.jvm.proxy.jdk.IHello;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;

import java.lang.reflect.InvocationTargetException;

/**
 * @author Minghua Chen
 * @date 2020-08-29 23:19
 */
public class AssitProx {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        ProxyFactory factory = new ProxyFactory();
        factory.setSuperclass(Hello.class);
        factory.setFilter(method -> method.getName().equals("sayHello"));

//
//        factory.setHandler((o, method, pro, objects) -> {
//            System.out.println("前置处理:");
//            Object result = pro.invoke(o, args);
//            System.out.println("执行结果:" + result);
//            System.out.println("后置处理");
//            return result;
//        });
//
//        Class<?> factoryClass = factory.createClass();
//        IHello instance = (IHello) factoryClass.newInstance();


        MethodHandler methodHandler = (self, thisMethod, proceed, args1) -> {
            System.out.println("前置处理:");
            Object result = proceed.invoke(self, args1);
            System.out.println("执行结果:" + result);
            System.out.println("后置处理");
            return result;

        };


        IHello instance = (IHello)factory.create(new Class[0], new Object[0], methodHandler);

        System.out.println(instance.getClass());
        instance.sayHello();
        System.out.println("处理完毕");
    }
}
